package com.arges.web.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 数据库密码加密算法和用户密码加密算法
 * 
 * @author arges
 * 
 */
@SuppressWarnings("restriction")
public class AESUtils
{
	/**
	 * 加密密码位数
	 */
	private static final int KEY_128 = 128;

	/**
	 * 加密秘钥
	 */
	public static final String PASSWORD_KEY = "Admin@123";

	/**
	 * 密码加密算法(JDk自带)
	 * 
	 * @param String
	 * @return
	 */
	// public static String toSHA256(String s)
	// {
	// MessageDigest messageDigest = null;
	// try
	// {
	// // 对密码加密算法 MD5 SHA-512 SHA-324 SHA-1 SHA-128
	// messageDigest = MessageDigest.getInstance("SHA-256");
	// messageDigest.update(s.getBytes(Charset.defaultCharset()));
	// }
	// catch (NoSuchAlgorithmException e)
	// {
	// e.printStackTrace();
	// }
	// if (messageDigest != null)
	// {
	// return new String(Base64.encodeBase64(messageDigest.digest()), Charset.defaultCharset());
	// }
	// else
	// {
	// return null;
	// }
	// }

	public static String toSHA256(String message)
	{
		String digest = null;
		try
		{
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] hash = md.digest(message.getBytes("UTF-8"));

			StringBuilder sb = new StringBuilder(2 * hash.length);
			for (byte b : hash)
			{
				sb.append(String.format("%02x", b & 0xff));
			}

			digest = sb.toString();

		}
		catch (Exception ex)
		{
		}
		return digest;
	}

	/**
	 * AES加密算法
	 * 
	 * @param content 加密内容
	 * @param password 加密秘钥
	 * @return
	 */
	public static String encrypt(String content, String password)
	{
		String result = null;
		try
		{
			SecretKey secretKey = generateKey(password);
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			byte[] encryString = cipher.doFinal(content.getBytes());
			BASE64Encoder base64Encoder = new BASE64Encoder();
			result = base64Encoder.encode(encryString);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * AES解密算法
	 * 
	 * @param content 解密内容
	 * @param password 解密秘钥
	 * @return
	 */
	public static String decrypt(String content, String password)
	{
		String result = null;
		try
		{
			SecretKey secretKey = generateKey(password);
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			BASE64Decoder base64Decoder = new BASE64Decoder();
			byte[] decryString = cipher.doFinal(base64Decoder.decodeBuffer(content));
			result = new String(decryString);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获得密钥
	 * 
	 * @param secretKey
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeyException
	 * @throws InvalidKeySpecException
	 */
	private static SecretKey generateKey(String secretKey) throws Exception
	{

		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes());
		keyFactory.generateSecret(keySpec);
		return keyFactory.generateSecret(keySpec);
	}

	public static void main(String[] args)
	{
		System.out.println(toSHA256("123456"));
		String enContent = "password";
		System.out.println(AESUtils.encrypt(enContent, AESUtils.PASSWORD_KEY));

		String deConent = "dgcgOol2htyKFzkQOmF3Jg==";
		System.out.println(AESUtils.decrypt(deConent, AESUtils.PASSWORD_KEY));
	}
}
